/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.valicode;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.web.BaseController;
import com.server.common.utils.StringUtils;
import com.server.modules.sys.entity.valicode.ValiCode;
import com.server.modules.sys.service.valicode.ValiCodeService;

/**
 * 验证码Controller
 * @author 颜洪毅
 * @version 2016-08-15
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/valicode/valiCode")
public class ValiCodeController extends BaseController {

	@Autowired
	private ValiCodeService valiCodeService;
	
	@ModelAttribute
	public ValiCode get(@RequestParam(required=false) String id) {
		ValiCode entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = valiCodeService.get(id);
		}
		if (entity == null){
			entity = new ValiCode();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:valicode:valiCode:view")
	@RequestMapping(value = {"list", ""})
	public String list(ValiCode valiCode, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ValiCode> page = valiCodeService.findPage(new Page<ValiCode>(request, response), valiCode); 
		model.addAttribute("page", page);
		return "modules/sys/valicode/valiCodeList";
	}

	@RequiresPermissions("sys:valicode:valiCode:view")
	@RequestMapping(value = "form")
	public String form(ValiCode valiCode, Model model) {
		model.addAttribute("valiCode", valiCode);
		return "modules/sys/valicode/valiCodeForm";
	}

	@RequiresPermissions("sys:valicode:valiCode:edit")
	@RequestMapping(value = "save")
	public String save(ValiCode valiCode, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, valiCode)){
			return form(valiCode, model);
		}
		valiCodeService.save(valiCode);
		addMessage(redirectAttributes, "保存验证码成功");
		return "redirect:"+Global.getAdminPath()+"/sys/valicode/valiCode/?repage";
	}
	
	@RequiresPermissions("sys:valicode:valiCode:edit")
	@RequestMapping(value = "delete")
	public String delete(ValiCode valiCode, RedirectAttributes redirectAttributes) {
		valiCodeService.delete(valiCode);
		addMessage(redirectAttributes, "删除验证码成功");
		return "redirect:"+Global.getAdminPath()+"/sys/valicode/valiCode/?repage";
	}

}